// 封装的是具体的接口请求方法
import request from '@/utils/request'

/**
 * 注册接口
 * @param {*} param0 { username：用户名, password：密码, repassword：第二次输入密码 }
 * @returns Promise对象
 */
export const registerAPI = ({ username, password, repassword }) => {
  return request({
    url: '/api/reg',
    method: 'POST',
    // axios传参 params、data
    // params的对象参数名和值，axios会把参数和值拼接在url?后面给后台（query查询字符串）
    // data的对象参数名和值，axios会把参数和值拼接在请求体里（body参数）
    data: {
      username: username,
      password: password,
      repassword: repassword
    }
  })
}

/**
 * 登录接口
 * @param {*} param0 { username：用户名, password：密码 }
 * @returns Promise对象
 */
export const loginAPI = ({ username, password }) => {
  return request({
    url: '/api/login',
    method: 'POST',
    data: {
      username,
      password
    }
  })
}

/**
 * 获取用户信息接口
 * @returns Promise对象
 */
export const getUserInfoAPI = () => {
  return request({
    url: '/my/userinfo',
    method: 'GET'
  })
}

/**
 * 获取侧边栏数据
 * @returns Promise对象
 */
export const getMenuListAPI = () => {
  return request({
    url: '/my/menus',
    method: 'GET'
  })
}

/**
 * 更新用户资料
 * @param {*} param0 { id, username:用户名, nickname:昵称, email:邮箱, userPic:用户头像 }
 * @returns Promise对象
 */
export const updateUserInfoAPI = ({ id, username, nickname, email, userPic }) => {
  return request({
    url: '/my/userinfo',
    method: 'PUT',
    data: {
      id,
      username,
      nickname,
      email,
      user_pic: userPic
    }
  })
}

/**
 * 更新用户头像
 * @param {*} avatar: 头像图片的base64字符串
 * @returns Promise对象
 */
export const updateAvatarAPI = (avatar) => {
  return request({
    url: '/my/update/avatar',
    method: 'PATCH',
    data: {
      avatar
    }
  })
}

/**
 * 更改用户密码
 * @param {*} param0 { oldPwd:原密码, newPwd:新密码, rePwd:确认密码 }
 * @returns Promise对象
 */
export const updatePwdAPI = ({ oldPwd, newPwd, rePwd }) => {
  return request({
    url: '/my/updatepwd',
    method: 'PATCH',
    data: {
      old_pwd: oldPwd,
      new_pwd: newPwd,
      re_pwd: rePwd
    }
  })
}

/**
 * 获取文章分类列表
 * @returns Promise对象
 */
export const getCategoryAPI = () => {
  return request({
    url: '/my/cate/list',
    method: 'GET'
  })
}

/**
 * 添加文章分类
 * @param {*} param0{ cateName:类别名称, cateAlias:类别别名 }
 * @returns Promise对象
 */
export const addCateAPI = ({ cateName, cateAlias }) => {
  return request({
    url: '/my/cate/add',
    method: 'POST',
    data: {
      cate_name: cateName,
      cate_alias: cateAlias
    }
  })
}

/**
 * 修改文章分类名
 * @param {*} param0 { id:文章分类id, cateName:文章类名, cateAlias:类别别名 }
 * @returns Promise对象
 */
export const updateCateAPI = ({ id, cateName, cateAlias }) => {
  return request({
    url: '/my/cate/info',
    method: 'PUT',
    data: {
      id,
      cate_name: cateName,
      cate_alias: cateAlias
    }
  })
}

/**
 * 删除文章类别
 * @param {*} id: 文章类别id
 * @returns Promise对象
 */
export const deleteCateAPI = (id) => {
  return request({
    url: '/my/cate/del',
    method: 'DELETE',
    params: {
      id
    }
  })
}

/**
 * 发布文章
 * @param {*} formData 表单数据对象
 * @returns Promise对象
 */
export const uploadArticleAPI = (formData) => {
  return request({
    url: '/my/article/add',
    method: 'POST',
    data: formData
  })
}

/**
 * 获取文章列表
 * @param {*} param0 { pagenum:当前页码, pagesize:当前页需要的数据条数, cateId:文章类别ID, state:文章状态 }
 * @returns Promise对象
 */
export const getArticalListAPI = ({ pagenum, pagesize, cateId, state }) => {
  return request({
    url: '/my/article/list',
    method: 'GET',
    params: {
      pagenum,
      pagesize,
      cate_id: cateId,
      state
    }
  })
}

/**
 * 获取文章详情
 * @param {*} id:文章id
 * @returns Promise对象
 */
export const getArticleInfoAPI = (id) => {
  return request({
    url: '/my/article/info',
    method: 'GET',
    params: {
      id
    }
  })
}

/**
 * 删除文章
 * @param {*} id:文章ID
 * @returns Promise对象
 */
export const deleteArticleAPI = (id) => {
  return request({
    url: '/my/article/info',
    method: 'DELETE',
    params: {
      id
    }
  })
}
